package main

import (
	"fmt"
	"io"
	"net/http"
)

/*
   Author: mahaiyuan
   Date: 2021/6/4 8:11 下午
   Description:
*/
func main() {
	resp, err := http.Get("http://127.0.0.1:8888/hello")
	if err != nil {
		fmt.Println("http get error.", err)
		return
	}
	defer resp.Body.Close()
	fmt.Printf("status=%s \n", resp.Status)
	// 读取响应结果
	data := make([]byte, 0, 1024)
	for true {
		buf := make([]byte, 20)	//每次读取20字节
		n, err := resp.Body.Read(buf)
		if n > 0 {
			data = append(data, buf[:n]...)	//追加数据
		}
		if err == io.EOF {	//读取到数据结尾
			println("read response finish.")
			break
		}
		if err != nil && err != io.EOF {	//发生异常，直接返回
			println("read response error.", err)
			return
		}
	}
	fmt.Printf("data size = %d\n", len(data))
	fmt.Println("response=", string(data))
}
